package com.kvest.scanrecognizer.ui.fragment;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.kvest.scanrecognizer.R;
import com.kvest.scanrecognizer.utility.Utils;
import uk.co.senab.photoview.PhotoView;

import java.util.LinkedList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Kvest
 * Date: 04.10.13
 * Time: 21:47
 * To change this template use File | Settings | File Templates.
 */
public class PreviewFragment extends Fragment {
    private static final int ROTATION_ANGLE_DELTA = 90;

    private PhotoView previewContent;

    private List<String> filesQueue = new LinkedList<String>();
    private String shownFile = null;
    private int rotation = 0;

    private int width;
    private int height;

    private OnFileSelectedListener onFileSelectedListener;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.preview_fragment, container, false);

        previewContent = (PhotoView)view.findViewById(R.id.preview_content);
        previewContent.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

        DisplayMetrics metrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
        width = metrics.widthPixels / 2;
        height = metrics.heightPixels / 2;

        //set listeners
        view.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancel();
            }
        });
        view.findViewById(R.id.rotate_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rotateLeft();
            }
        });
        view.findViewById(R.id.rotate_right).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rotateRight();
            }
        });
        view.findViewById(R.id.preview_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                previewOk();
            }
        });

        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        if (shownFile != null) {
            setContent();
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        try {
            onFileSelectedListener = (OnFileSelectedListener)activity;
        } catch (ClassCastException cce) {
            onFileSelectedListener = null;
        }
    }

    private void cancel() {
        //try show next preview image
        setNextPreview();
    }

    private void rotateLeft() {
        rotation = (rotation - ROTATION_ANGLE_DELTA) % 360;
        rotate();
    }

    private void rotateRight() {
        rotation = (rotation + ROTATION_ANGLE_DELTA) % 360;
        rotate();
    }

    private void previewOk() {
        if (onFileSelectedListener != null) {
            onFileSelectedListener.onFileSelected(shownFile, rotation);
        }

        //try show next preview image
        setNextPreview();
    }

    private void setNextPreview() {
        if (filesQueue.isEmpty()) {
            getActivity().finish();
        } else {
            shownFile = filesQueue.remove(0);
            setContent();
        }
    }

    public void addPreview(String fileName) {
        if (shownFile == null) {
            shownFile = fileName;

            setContent();
        } else {
            filesQueue.add(fileName);
        }
    }

    private void setContent() {
        if (previewContent != null) {
            //set image
            Bitmap image = Utils.createThumbnailImage(shownFile, width, height);
            previewContent.setImageBitmap(image);

            //reset rotation
            rotation = 0;
            rotate();
        }
    }

    private void rotate() {
        previewContent.setPhotoViewRotation(-rotation);
    }

    public interface OnFileSelectedListener {
        public void onFileSelected(String fileName, int rotation);
    }
}
